// pages/sharebox/deviceList/deviceList.js
var app = getApp();
var _PAGE = 1;
var _PAGESIZE = 10;
var util = require("../../../utils/util.js");
var load= true;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isActive: 1,
    hasMore: false,
    loading: false,
    deviceList: [],
    msgList: []
  },

  /**relatio
   * sasdsad
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var that = this;
    that.getdeviceList();
    that.getMsg();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    var that = this;
    if (!that.data.hasMore) {
      return false;
    }
    _PAGE++;
    that.setData({
      loading: true
    })
    if (that.data.isActive == 1) {
      that.getdeviceList();
    }
    if (that.data.isActive == 2) {
      that.getMsg();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },

  // 切换选项卡
  changetab(e) {
    var that = this;
    var isActive = e.currentTarget.dataset.type;
    _PAGE = 1;
    if (isActive == 1) {
      that.getdeviceList();
    }
    if (isActive == 2) {
      that.getMsg();
    }
    that.setData({
      isActive: e.currentTarget.dataset.type
    })
  },

  // 获取缴费器列表
  getdeviceList() {
    var that = this;
    app.ajax({
      url: "User/Shared/sharedDeviceList",
      data: {
        page: _PAGE,
        row: _PAGESIZE,
      },
      success: function(res) {
        if (res.data.code == 1000) {
          console.log(res.data.data)
          if (_PAGE == 1) {
            that.setData({
              deviceList: res.data.data.data
            })
          } else {
            that.setData({
              deviceList: that.data.deviceList.concat(res.data.data.data)
            })
          }
          // 是否加载更多
          var hasMore = true;
          if (res.data.data.data.length < _PAGESIZE) {
            hasMore = false;
          }
          that.setData({
            hasMore: hasMore,
            loading: false
          });
          load= false;
        } else {
          if (_PAGE == 1) {
            that.setData({
              hasMore: false,
              loading: false
            })
          } else {
            that.setData({
              hasMore: false,
              loading: false
            })
          }
        }
      }
    })
  },

  getMsg() {
    var that = this;
    app.ajax({
      url: "User/Shared/sharedMessage",
      data: {
        page: _PAGE,
        row: _PAGESIZE,
      },
      success: function(res) {
        if (res.data.code == 1000) {
          console.log(res.data.data)
          if (_PAGE == 1) {
            that.setData({
              msgList: res.data.data.data
            })
          } else {
            that.setData({
              msgList: that.data.msgList.concat(res.data.data.data)
            })
          }
          // 是否加载更多
          var hasMore = true;
          if (res.data.data.data.length < _PAGESIZE) {
            hasMore = false;
          }
          that.setData({
            hasMore: hasMore,
            loading: false
          });
          load= false;
        } else {
          if (_PAGE == 1) {
            that.setData({
              hasMore: false,
              loading: false
            })
          } else {
            that.setData({
              hasMore: false,
              loading: false
            })
          }
        }
      }
    })
  },

  // 已共享缴费器
  shared(e) {
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../shareDevDetail/shareDevDetail?id=' + id
    })
  },

  // 未共享缴费器
  nshared(e) {
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../pattern/pattern?id=' + id
    })
  },

  //跳转共享用户
  goShareUser: function(e) {
    var id = e.currentTarget.dataset.msgid;
    var state = e.currentTarget.dataset.state;
    var aimg = e.currentTarget.dataset.aimg;
    var dname = e.currentTarget.dataset.dname;
    var name = e.currentTarget.dataset.name;
    if (state == true) {
      wx.navigateTo({
        url: '../shareUser/shareUser?id=' + id + '&aimg=' + aimg + '&dname=' + dname + '&name=' + name + '&tab=1'
      })
    } else {
      app.showToast('该消息已被处理，不能重复操作');
    }
  },
  //跳转到新增页面
  chooseFacility() {
    var token = wx.getStorageSync('token');
    //添加缴费器必须登录
    if (!util.isExitsVariable(token)) {
      wx.navigateTo({
        url: '/pages/user/login/login',
      })
      return false;
    }
    wx.navigateTo({
      url: '../../otherLogin/otherLogin',
    })
  },

})
